
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>c:forEach</title>
        <style type="text/css">
            h3 , p { text-align: center ; }
            .container { border: 1px solid green ; }
            .wrapper { display: flex ; border: 1px solid blue ; margin: 15px ; }
            .wrapper>* { flex-basis: 100px ; background: #ffe59c; }
            .wrapper>*:nth-child(2n) { background: #dedede ; }
        </style>
    </head>
    <body>

        <h3>c:forEach</h3>

        <hr>

        <p>当作普通for循环使用</p>
        <div class="wrapper">
            <c:forEach begin="0" step="1" end="10" var="count">
                <span>${ count }</span>
            </c:forEach>
        </div>

        <div class="wrapper">
            <% for (int i = 0; i <= 10 ; i++) {
                pageContext.setAttribute( "count" , i );
            %>
                <span>${ count }</span>
            <% } %>
        </div>

        <hr>

        <p>遍历数组</p>
        <%
            int[] array = { 9 , 4 , 5 , 2 , 3 , 7 , 6 , 1 , 8 };
            pageContext.setAttribute( "array" , array );
        %>

        <div class="wrapper">
            <c:forEach items="${ array }" var="element">
                <span>${ element }</span>
            </c:forEach>
        </div>

        <div class="wrapper">
            <% for (int i = 0; i < array.length ; i++) {
                int element = array[ i ] ;
                pageContext.setAttribute( "element" , element );
            %>
            <span>${ element }</span>
            <% } %>
        </div>

        <hr>

        <p>迭代Collection集合</p>
        <%
            Set<String> collection = new HashSet<>();
            String[] nameArray = { "曾子豪" , "莫雨峰" , "姜禹志" , "刘绍骏" };
            collection.addAll( Arrays.asList( nameArray ) );
            pageContext.setAttribute( "names" , collection );
        %>
        <div class="wrapper">
            <c:forEach items="${ names }" var="name">
                <span>${ name }</span>
            </c:forEach>
        </div>

        <hr>

        <p>迭代Map集合</p>
        <%
            Map<String,Integer> map = new HashMap<>();
            map.put( "回锅肉" , 18 );
            map.put( "盐煎肉" , 20 );
            map.put( "红烧肉" , 22 );
            map.put( "肘子乐开花" , 38 );
            pageContext.setAttribute( "map" , map );
        %>

        <div class="wrapper">
            <c:forEach items="${ map }" var="entry">
                <span>${ entry.key } : ${ entry.value } </span>
            </c:forEach>
        </div>

        <div class="wrapper">
            <%
                Set< Map.Entry<String,Integer> > entries = map.entrySet();
                Iterator< Map.Entry<String,Integer> > iterator = entries.iterator();
                while( iterator.hasNext() ){
                    Map.Entry<String,Integer> entry = iterator.next();
                    pageContext.setAttribute( "entry" , entry );
            %>
                    <span>${ entry.key } : ${ entry.value } </span>
            <%  } %>
        </div>

        <hr>

        <p>LoopTagStatus</p>
        <div class="container">
            <%-- varStatus 属性的值是咱们指定的，不是非要是某个值 --%>
            <c:forEach begin="1" step="2" end="10" var="counter" varStatus="state">
                <div class="wrapper">
                    <span>${ state.current }</span>
                    <span>${ state.first }</span>
                    <span>${ state.index }</span>
                    <span>${ state.count }</span>
                    <span>${ state.begin }</span>
                    <span>${ state.step }</span>
                    <span>${ state.end }</span>
                    <span>${ state.last }</span>
                    <span>${ state.current == counter }</span>
                </div>
            </c:forEach>
        </div>

        <div class="container">
            <c:forEach items="${ names }" var="name" varStatus="state">
                <div class="wrapper">
                    <span>${ state.current }</span>
                    <span>${ state.first }</span>
                    <span>${ state.index }</span>
                    <span>${ state.count }</span>
                    <span>${ state.begin }</span>
                    <span>${ state.step }</span>
                    <span>${ state.end }</span>
                    <span>${ state.last }</span>
                    <span>${ state.current == name }</span>
                </div>
            </c:forEach>
        </div>


    </body>
</html>
